package com.jackrain.nea.ext.permission;

import java.io.Serializable;
import java.util.HashMap;

/**
 * @author lzm
 * @date 2017-12-8
 */
public class InDevPermissionCollection extends PermissionCollection implements Serializable {
    private static final long serialVersionUID = 1021002056138255168L;
    private MenuCollection tree;
    private PermissionCollection innerCollection;
    private HashMap<Long, MenuPermissionModel> inDevModeMenuCache = new HashMap<>();  //菜单项权限集合
    public InDevPermissionCollection(MenuCollection tree, PermissionCollection innerCollection){
        this.tree = tree;
        this.innerCollection = innerCollection;
    }

    @Override
    public void addAction(ActionPermissionModel model) {
        //do nth.
    }

    @Override
    public void addMenu(MenuPermissionModel model) {
        //do nth.
    }

    @Override
    public MenuPermissionModel fetchMenuByMenuId(Long menuId) {
        MenuNode node = tree.fetchById(MenuNodeType.MENU, menuId);
        if(node != null && node.isIsdev()){
            MenuPermissionModel model = inDevModeMenuCache.get(menuId);
            if(model == null){
                model = createFullMenuPermission(-1L, node);
                inDevModeMenuCache.put(menuId, model);
            }
            return model;
        }
        return innerCollection.fetchMenuByMenuId(menuId);
    }

    @Override
    public ActionPermissionModel fetchActionById(Long actionId) {
        return innerCollection.fetchActionById(actionId);
    }
}
